﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using Telerik.Web.UI;

public partial class Admin_UserControls_PostManagement : System.Web.UI.UserControl
{
    private static int PostID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
    }

    protected void RadGrid1_iTemcommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Xem")
        {
            GridDataItem item = (GridDataItem)e.Item;
            PostID = int.Parse(item["post_id"].Text.ToString());
            Post post = Post.GetInfo(PostID);
            Group group = Group.GetInfo(post.Group_id);
            Label_Group.Text = group.Group_name;
            Label_Content.Text = post.Text;
            Label_DateCreate.Text = String.Format("{0,2:00}:{1,2:00}:{2,2:00} - {3,2:00}/{4,2:00}/{5,4}",
                post.Created_date.Hour, post.Created_date.Minute, post.Created_date.Second,
                post.Created_date.Day, post.Created_date.Month, post.Created_date.Year);
            Panel1.Visible = false;
            Panel2.Visible = true;
            return;
        }

        if (e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            Post.Delete(int.Parse(item["post_id"].Text));
            return;
        }
        if(e.CommandName == "CommentManager")
        {
            GridDataItem item = (GridDataItem)e.Item;
            PostID = int.Parse(item["post_id"].Text.ToString());
            Post post = Post.GetInfo(PostID);
            Group group = Group.GetInfo(post.Group_id);
            Label_Group.Text = group.Group_name;
            Label_Content.Text = post.Text;
            Label_DateCreate.Text = String.Format("{0,2:00}:{1,2:00}:{2,2:00} - {3,2:00}/{4,2:00}/{5,4}",
                post.Created_date.Hour, post.Created_date.Minute, post.Created_date.Second,
                post.Created_date.Day, post.Created_date.Month, post.Created_date.Year);
            Panel1.Visible = false;
            Panel2.Visible = true;
            return;
        }

    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        DataTable dt = Post.GetAll();
        dt.Columns.Add("groupname");
        foreach (DataRow dtRow in dt.Rows)
        {
            Group group = Group.GetInfo((int)dtRow["group_id"]);
            dtRow["groupname"] = group.Group_name;
            String Content = dtRow["text"].ToString();
            if (Content.Length > 200)
            {
                Content = Content.Substring(0, 200).Trim();
                Content = Content.Substring(0, Content.LastIndexOf(' '));
                Content += "...";
            }
            dtRow["text"] = Content;
        }

        DataView dv = dt.DefaultView;
        dv.Sort = "created_date DESC";
        RadGrid1.DataSource = dv.ToTable();
        RadGrid1.DataBind();
    }
    protected void RadGrid2_PreRender(object sender, EventArgs e)
    {
        DataTable dt = CommentPost.GetAll(PostID);
        dt.Columns.Add("username");
        foreach (DataRow dtRow in dt.Rows)
        {
            Member member = Member.GetInfo((int)dtRow["member_id"]);
            User user = User.GetInfo(member.Member_user_id);
            dtRow["username"] = user.UserName;
        }
        DataView dv = dt.DefaultView;
        dv.Sort = "created_date DESC";
        RadGrid2.DataSource = dv.ToTable();
        RadGrid2.DataBind();
    }
    protected void RadGrid2_iTemcommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Xem")
        {
            return;
        }

        if (e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            CommentPost.Delete(int.Parse(item["comment_id"].Text));
            return;
        }
    }
    protected void Button_OK_Click(object sender, EventArgs e)
    {
        Post.Delete(PostID);
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
}
